package com.breakfast.fun.view.viewholder;

import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.breakfast.fun.R;
import com.breakfast.fun.adapter.home.HomeBannerAdapter;
import com.breakfast.fun.base.BaseViewHolder;
import com.breakfast.fun.data.entity.home.CenterAd;
import com.breakfast.fun.data.entity.mall.MallBanner;
import com.breakfast.fun.view.widget.ScrollHandler;
import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.Bind;

/**
 * Created by yukai on 16/10/17.
 */

public class ToolBannerVH extends BaseViewHolder<MallBanner.DataBean> {
    @Bind(R.id.vp_tool)
    ViewPager mVpTool;
    @Bind(R.id.iv_tool_ad)
    ImageView mIvToolAd;
    @Bind(R.id.tl_tool)
    TabLayout mTlTool;

    public int adCount = 0;
    private ScrollHandler handler;
    private List<CenterAd> centerAdList;
    private HomeBannerAdapter adapter;

    private Boolean isBind = false;

    public ToolBannerVH(View v) {
        super(v);
    }

    @Override
    public int getType() {
        return R.layout.layout_tool_banner;
    }

    @Override
    public void onBindViewHolder(View view, MallBanner.DataBean obj) {
        if (obj == null || obj.getBanner().size() == 0) {
            mTlTool.setVisibility(View.GONE);
            mVpTool.setVisibility(View.GONE);
            mIvToolAd.setVisibility(View.VISIBLE);
            mIvToolAd.setImageResource(R.drawable.bg_mall);
            return;
        }

        if (!isBind) {
            isBind = true;
            if (obj.getBanner().size() == 1) {
                mTlTool.setVisibility(View.GONE);
                mVpTool.setVisibility(View.GONE);
                mIvToolAd.setVisibility(View.VISIBLE);
                Glide.with(mContext)
                        .load(obj.getBanner().get(0).getPicture())
                        .placeholder(R.drawable.bg_cache_banner)
                        .error(R.drawable.bg_cache_banner)
                        .into(mIvToolAd);
            } else {
                mTlTool.setVisibility(View.VISIBLE);
                mVpTool.setVisibility(View.VISIBLE);
                mIvToolAd.setVisibility(View.GONE);
                addTab(obj.getBanner().size());
                adCount = obj.getBanner().size();
                if (obj.getBanner().size() == 2) {
                    obj.getBanner().add(obj.getBanner().get(0));
                    obj.getBanner().add(obj.getBanner().get(1));
                }

                this.centerAdList = obj.getBanner();
                adapter = new HomeBannerAdapter(mContext, obj.getBanner());
                mVpTool.setAdapter(adapter);

                handler = new ScrollHandler(mVpTool);

                mVpTool.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                        handler.sendMessage(Message.obtain(handler, ScrollHandler.MSG_PAGE_CHANGED, position, 0));
                        if (adCount == 2) {
                            mTlTool.getTabAt(position % centerAdList.size() % 2).select();
                        } else {
                            try {
                                mTlTool.getTabAt(position % centerAdList.size()).select();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {
                        switch (state) {
                            case ViewPager.SCROLL_STATE_DRAGGING:
                                //正在拖动页面时
                                handler.sendEmptyMessage(ScrollHandler.MSG_KEEP_SILENT);
                                break;
                            case ViewPager.SCROLL_STATE_IDLE:
                                //页面空闲时
                                handler.sendEmptyMessageDelayed(ScrollHandler.MSG_UPDATE_IMAGE, ScrollHandler.MSG_DELAY);
                                break;
                        }
                    }
                });

                //发送图片轮播消息
                handler.sendEmptyMessageDelayed(ScrollHandler.MSG_UPDATE_IMAGE, ScrollHandler.MSG_DELAY);


            }
        }
    }

    /**
     * 添加tablayout 子tab
     *
     * @param count
     */
    public void addTab(int count) {
        mTlTool.removeAllTabs();
        for (int i = 0; i < count; i++) {
            mTlTool.addTab(mTlTool.newTab());
        }
    }

}
